
export default function exportCsv(cols, data, fileName) {
    try {
        let content = "";
        _.each(cols, (col) => {
            col = col ? col.replace('"', "'") : "";
            content += `"${col}",`;
        });
        content = content.endsWith(",") ? content.substr(0, content.length - 1) : content;
        content += "\r\n";
        _.each(data, (rows) => {
            _.each(rows, (row) => {
                row = row ? row.replace('"', "'") : "";
                content += `"${row}",`;
            });
            content = content.endsWith(",") ? content.substr(0, content.length - 1) : content;
            content += "\r\n";
        });
        
        let csvContent = "data:text/csv;charset=GBK,\uFEFF" + content;
        let encodedUri = encodeURI(csvContent);
        let link = document.createElement("a");
        link.setAttribute("href", encodedUri);
        link.setAttribute("download", `${(fileName || 'file')}.csv`);
        document.body.appendChild(link); // Required for FF
        link.click(); // This will download the data file named "my_data.csv".
        document.body.removeChild(link); // Required for FF
    } catch (err) {
        // Errors are thrown for bad options, or if the data is empty and no fields are provided. 
        // Be sure to provide fields if it is possible that your data array will be empty. 
        console.error(err);
    }
}
